using System.Web;
using System.Xml;
using System;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington	
	Started:	2006-06-20	
	Status:		release	
	Version:	1.0.4.20060620
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/
	
namespace umlungu.engineX.blogX {
	/// <summary>Description of the enumeration</summary>
	public enum x_userprofilepattern {
		/// <summary>test process identifier</summary>
		deflt = 0,
		/// <summary>error test process identifier</summary>
		login = 1
	}
	/// <summary>
	/// Description of the x_userprofile class.
	/// <para>Additional information about the class</para>
	/// </summary>
	public class x_userprofile : x_profile {
		#region Invisible properties
		private const string filename = "userProfileX";
		private const string ELEMENT_PATTERNS = "patterns";
		private const string ELEMENT_PATTERN = "pattern";
		private const string PATTERN_LOGIN = "login";
		#endregion

		#region Visible properties
		/// <summary>The 'patterns' element</summary>
		/// <value>An XmlElement containing the profile patterns</value>
		public XmlElement Patterns {
			get { return Node(ELEMENT_PATTERNS); }
		}
		/// <summary>The login pattern element</summary>
		/// <value>The login pattern XmlElement in the profile's patterns</value>
		public string LoginPattern {
			get {
				XmlNode pat = getPattern(PATTERN_LOGIN);
				return (pat == null)? "" : pat.InnerText;
			}
			set {
				XmlNode pat = getPattern(PATTERN_LOGIN);
				if (pat != null)
					pat.InnerText = value;
			}
		}
		#endregion

		#region Constructors/Destructors
		/// <overloads>Constructor</overloads>
		/// <summary>Default constructor - typically not used</summary>
		public x_userprofile() : base("/", filename) {
		}
		/// <summary>Constructor that gives path to user profile file</summary>
		/// <param name="path">The path to the profile template file</param>
		public x_userprofile(string path) : base(path, filename) {
		}
		#endregion

		#region Public methods
		/// <summary>Removes all existing patterns and sets a single public pattern</summary>
		public void Logout() {
			Patterns.RemoveAll();
			AddPattern(PATTERN_LOGIN, "public");
		}
		/// <summary>Sets the value of an existing pattern</summary>
		public void SetPattern(string pattern, string value) {
			XmlNode pat = getPattern(pattern);
			if (pat != null) {
				pat.InnerText = value;
			} else {
				AddPattern(pattern, value);
			}
		}
		/// <summary>Sets the value of an existing pattern</summary>
		public void AddPattern(string pattern, string value) {
			XmlElement patEl = Patterns.AppendChild(ProfileXml.CreateElement(ELEMENT_PATTERN)) as XmlElement;
			patEl.InnerText = value;
			patEl.SetAttribute("name", pattern);
		}
		/// <summary>Sets the value of an existing pattern</summary>
		public void DeletePattern(string pattern) {
			XmlNode pat = getPattern(pattern);
			if (pat != null) {
				pat.ParentNode.RemoveChild(pat);
			}
		}
		/// <summary>Tests for the existence of a pattern</summary>
		public bool TestPattern(XmlElement testpattern) {
			/*
			XmlNode pat = getPattern(testpattern.GetAttribute("name"));
			if (pat != null)
				return (pat.InnerText == testpattern.InnerText);
			return false;
			*/
			return (getPattern(testpattern.GetAttribute("name"), testpattern.InnerText) != null);
		}
		#endregion
		
		#region Protected methods
		#endregion
				
		#region Private methods
		private XmlNode getPattern(x_userprofilepattern type) {
			XmlNode retNode = null;
			switch(type) {
				case x_userprofilepattern.login:	retNode = getPattern(PATTERN_LOGIN);	break;
			}
			return retNode;
		}
		private XmlNode getPattern(string name) {
			return Patterns.SelectSingleNode(String.Concat(ELEMENT_PATTERN, "[@name='", name, "']"));
		}
		private XmlNode getPattern(string name, string text) {
			return Patterns.SelectSingleNode(String.Concat(ELEMENT_PATTERN, "[@name='", name, "' and text()='", text, "']"));
		}
		#endregion
	}
}
